package com.next

import grails.converters.JSON
import grails.gorm.transactions.Transactional

@Transactional
class OpportunityContractService
{

    /**
     * @Description
     * @Author bigyuan
     * @Return
     * @DateTime 2017/11/16 16:05
     */
    //生成新合同号规则
    def createNewContractNo(Opportunity opportunity)
    {
        def map = [:]

        if (opportunity.contracts.size() > 0)
        {
            map.put("flag", "failed")
            map.put("memo", "${opportunity.serialNumber}此订单已经存在合同号无法生成")
        } else
        {
            //生成合同号规则
            def generateContractNumber = { loanDoor, productName, contractType, serialNumber ->
                def contractNumber = "ZJX"
                switch (loanDoor)
                {
                    case "外贸信托": contractNumber += "WM_"
                        break
                    case "中航信托": contractNumber += "ZH_"
                        break
                    case "中海信托": contractNumber += "ZS_"
                        break
                    case "渤海信托": contractNumber += "BH_"
                        break
                    case "中诚信托": contractNumber += "ZC_"
                        break
                }
                switch (productName)
                {
                    case "速e贷": contractNumber += "SE"
                        break
                    case "新e贷": contractNumber += "XE"
                        break
                    case "融e贷": contractNumber += "RE"
                        break
                    case "等额本息": contractNumber += "DE"
                        break
                    case "月息年本": contractNumber += "YX"
                        break
                }
                switch (contractType)
                {
                    case "借款合同": contractNumber += "DK_"
                        break
                    case "抵押合同": contractNumber += "DY_"
                        break
                    case "委托借款代理服务合同": contractNumber += "FW_"
                        break
                }
                def newSerialNumber = serialNumber.replace("-", "")

                contractNumber += newSerialNumber
                return contractNumber
            }

            def category = OpportunityFlexFieldCategory.find("from OpportunityFlexFieldCategory where opportunity.id = ? and flexFieldCategory.id = ?",[opportunity?.id,FlexFieldCategory.find("from FlexFieldCategory where name = '资金渠道'")])
            def loanDoor1 = OpportunityFlexField.find("from  OpportunityFlexField where name = '放款通道' and category.id = ?",[category?.id])?.value
            def productName1 = opportunity?.product?.name
            if (loanDoor1 && loanDoor1 in ["外贸信托", "中航信托", "中海信托", '渤海信托', '中诚信托'] && productName1 && productName1 in ['速e贷', '新e贷', '融e贷', '等额本息', "月息年本"])
            {
                def list = []
                def opportunityContract = new com.next.OpportunityContract(opportunity: opportunity, contract: new com.next.Contract(type: com.next.ContractType.findByName("借款合同")))
                opportunityContract.contract.serialNumber = generateContractNumber(loanDoor1, productName1, opportunityContract?.contract?.type?.name, opportunity.serialNumber)
                if (opportunityContract.validate())
                {
                    opportunityContract.save flush: true
                } else
                {
                    println(opportunityContract.errors)
                }
                list.add(opportunityContract.contract.serialNumber)
                opportunityContract = new com.next.OpportunityContract(opportunity: opportunity, contract: new com.next.Contract(type: com.next.ContractType.findByName("抵押合同")))
                opportunityContract.contract.serialNumber = generateContractNumber(loanDoor1, productName1, opportunityContract?.contract?.type?.name, opportunity.serialNumber)
                if (opportunityContract.validate())
                {
                    opportunityContract.save flush: true
                } else
                {
                    println(opportunityContract.errors)
                }
                list.add(opportunityContract.contract.serialNumber)
                opportunityContract = new com.next.OpportunityContract(opportunity: opportunity, contract: new com.next.Contract(type: com.next.ContractType.findByName("委托借款代理服务合同")))
                opportunityContract.contract.serialNumber = generateContractNumber(loanDoor1, productName1, opportunityContract?.contract?.type?.name, opportunity.serialNumber)
                if (opportunityContract.validate())
                {
                    opportunityContract.save flush: true
                } else
                {
                    println(opportunityContract.errors)
                }
                list.add(opportunityContract.contract.serialNumber)

                def result = "生成合同号："
                list.each {
                    result += "${it},"
                }
                map.put("flag", "success")
                map.put("memo", "订单${opportunity.serialNumber}_${result}")
            } else
            {
                map.put("flag", "failed")
                map.put("memo", "订单选择产品和放款通道不符合合同号生成规则")
            }
        }

        return map as JSON
        //生成合同号

    }
}
